import 'mint-ui/lib/style.css'
//import EXIF from './exif.js'
var ex = require('./exif.js')
import {Indicator, Toast} from 'mint-ui'

class Utils {
  constructor(imageObj) {
    this.image = {}
    this.targetImage = imageObj
    /*this.fileNames = []
    this.deleteImage = function (index) {
      this.fileNames.splice(index, 1)
    }*/
  }

  loadFile(event, postFunc, size, scrrenShot) {
    var file = scrrenShot || event.target.files[0],
      fileSize = file.size / 1024 / 1024,
      imgUrl = window.URL.createObjectURL(file),
      Orientation = null;
    /*// 上传过的图片不再进行上传
    if (this.fileNames.includes(file.name)) {
      return
    } else {
      this.fileNames.push(file.name)
    }*/

    // 限制上传图片的大小
    if (fileSize > size) {
      Toast({
        message: '图片过大',
        className: 'toast-style'
      })
      return
    }

    if (file) {
      var rFilter = /^(image\/jpeg|image\/png)$/i // 检查图片格式
      if (!rFilter.test(file.type)) {
        Indicator.close()
        return null
      }

      // file = document.getElementById('fileBtn').files[0]
      //获取照片方向角属性，用户旋转控制
      ex['EXIF'].getData(file, function () {
        Orientation = ex['EXIF'].getTag(this, 'Orientation')
        //alert('Orientation1: ' + Orientation)
        Indicator.open()

        var image = new Image()
        image.src = imgUrl
        image.onload = function () {
          var expectWidth = this.naturalWidth
          var expectHeight = this.naturalHeight

          if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
            expectWidth = 1000
            expectHeight = expectWidth * this.naturalHeight / this.naturalWidth
          } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
            expectHeight = 1400
            expectWidth = expectHeight * this.naturalWidth / this.naturalHeight
          }
          var canvas = document.createElement('canvas')
          var ctx = canvas.getContext('2d')
          canvas.width = expectWidth
          canvas.height = expectHeight
          ctx.drawImage(this, 0, 0, expectWidth, expectHeight)
          var base64 = null
          var quality = 0.1
          //修复ios
          if (navigator.userAgent.match(/iphone/i)) {
            //alert(expectWidth + ',' + expectHeight);
            //如果方向角不为1，都需要进行旋转 added by lzk
            if (Orientation != '' && Orientation != 1) {
              switch (Orientation) {
                case 6://需要顺时针（向左）90度旋转
                  rotateImg(this, 'left', canvas)
                  break
                case 8://需要逆时针（向右）90度旋转
                  rotateImg(this, 'right', canvas)
                  break
                case 3://需要180度旋转
                  rotateImg(this, 'right', canvas)//转两次
                  rotateImg(this, 'right', canvas)
                  break
              }
            }
            base64 = canvas.toDataURL('image/jpeg', quality)
          } else if (navigator.userAgent.match(/Android/i)) {// 修复android
            base64 = canvas.toDataURL('image/jpeg', quality)
          } else {
            //alert(Orientation);
            if (Orientation != '' && Orientation != 1) {
              //alert('旋转处理');
              switch (Orientation) {
                case 6://需要顺时针（向左）90度旋转
                  rotateImg(this, 'left', canvas)
                  break
                case 8://需要逆时针（向右）90度旋转
                  rotateImg(this, 'right', canvas)
                  break
                case 3://需要180度旋转
                  rotateImg(this, 'right', canvas)//转两次
                  rotateImg(this, 'right', canvas)
                  break
              }
            }

            base64 = canvas.toDataURL('image/jpeg', quality)
          }
          var _callback = {}
          _callback.src = base64
          _callback.imgUrl = imgUrl
          _callback.name = file.name
          Indicator.close()
          postFunc(_callback)
        }

      })
    }
  }
}

//对图片旋转处理 added by lzk
function rotateImg(img, direction, canvas) {
  //alert(img);
  //最小与最大旋转方向，图片旋转4次后回到原方向
  var min_step = 0
  var max_step = 3
  //var img = document.getElementById(pid);
  if (img == null) return
  //img的高度和宽度不能在img元素隐藏后获取，否则会出错
  var height = img.height
  var width = img.width
  //var step = img.getAttribute('step');
  var step = 2
  if (step == null) {
    step = min_step
  }
  if (direction == 'right') {
    step++
    //旋转到原位置，即超过最大值
    step > max_step && (step = min_step)
  } else {
    step--
    step < min_step && (step = max_step)
  }
  //旋转角度以弧度值为参数
  var degree = step * 90 * Math.PI / 180
  var ctx = canvas.getContext('2d')
  switch (step) {
    case 0:
      canvas.width = width
      canvas.height = height
      ctx.drawImage(img, 0, 0)
      break
    case 1:
      canvas.width = height
      canvas.height = width
      ctx.rotate(degree)
      ctx.drawImage(img, 0, -height)
      break
    case 2:
      canvas.width = width
      canvas.height = height
      ctx.rotate(degree)
      ctx.drawImage(img, -width, -height)
      break
    case 3:
      canvas.width = height
      canvas.height = width
      ctx.rotate(degree)
      ctx.drawImage(img, -width, 0)
      break
  }
}

export default Utils
